package cn.com.nxt.txb;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import cn.com.nxt.txb.httpdelegate.MeetingDelegate;
import cn.com.nxt.txb.model.User;
import cn.com.nxt.txb.util.AppUtil;
import cn.com.nxt.txb.util.LogoutHelper;

@ContentView(R.layout.activity_meeting_set)
public class MeetingSetActivity extends BaseActivity {
    public static final byte RESULT_INITIATION_PASSWORD = 0x003;//修改入会密码时返回
    public static final byte RESULT_COMPERE_PASSWORD = 0x004;//修改主持人密码时返回
    public static final byte RESULT_SILENT_MODE = 0x005;//修改静音模式时返回
    @ViewInject(R.id.toolbar)
    private Toolbar toolbar;
    //入会密码
    @ViewInject(R.id.tv_initiation_password)
    private TextView mInitiationPasswordTv;
    //主持人密码
//    @ViewInject(R.id.tv_compere_password)
//    private TextView mComperePasswordTv;
    //入会静音模式
    @ViewInject(R.id.tv_silent_mode)
    private TextView mSilentModeTv;
    private MeetingDelegate delegate;
    public static int model=-1;
    public static String password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);//注解绑定
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        delegate=new MeetingDelegate(this);
        delegate.meetingSettingInfo(AppUtil.mUser.getToken(),RESULT_INITIATION_PASSWORD);
    }

    @Event({R.id.ll_initiation_password,R.id.ll_compere_password,R.id.ll_silent_mode})
    private void onViewClick(View v) {
        switch (v.getId()) {
            case R.id.ll_initiation_password:
                initiationPassword();
                break;
            case R.id.ll_compere_password:
                comperePassword();
                break;
            case R.id.ll_silent_mode:
                silentMode();
                break;
        }
    }

    private void silentMode() {
        Intent intent = new Intent(MeetingSetActivity.this, SilentModeActivity.class);
        startActivityForResult(intent, RESULT_SILENT_MODE);
    }

    private void comperePassword() {
        Intent intent = new Intent(MeetingSetActivity.this, MeetingPasswordActivity.class);
        intent.putExtra("compere_password",true);
        startActivityForResult(intent, RESULT_COMPERE_PASSWORD);
    }

    private void initiationPassword() {
        Intent intent = new Intent(MeetingSetActivity.this, MeetingPasswordActivity.class);
        intent.putExtra("initiation_password",password);
        startActivityForResult(intent, RESULT_INITIATION_PASSWORD);
    }

    @Override
    public void onActivityResult(int arg0, int arg1, Intent data) {
        super.onActivityResult(arg0, arg1, data);
        if (data == null) {
            //h   vf' 'o,,[9[[8/showToast("--null--");
            return;
        }
        switch (arg0) {
            case RESULT_INITIATION_PASSWORD:
                mInitiationPasswordTv.setText(TextUtils.isEmpty(password)?"没有密码":password);
                break;
            case RESULT_SILENT_MODE:
                if(model==1)
                    mSilentModeTv.setText("智能静音");
                else if(model==2)
                    mSilentModeTv.setText("全部静音");
                break;
        }
    }

    @Override
    public void success(String result, byte flag) {
        super.success(result, flag);
        JSONObject obj= null;
        try {
            obj = new JSONObject(result);
            password=obj.optString("password");
            mInitiationPasswordTv.setText(password);
            model=obj.optInt("mute_model");
            if(model==1)
                mSilentModeTv.setText("智能静音");
            else if(model==2)
                mSilentModeTv.setText("全部静音");
        } catch (JSONException e) {
        }
    }

}
